﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using CityDashboard.Models;
using CityDashboard.Services;
using Microsoft.Practices.Prism.Mvvm;

namespace CityDashboard.ViewModels
{
    public class HoSoDangGiaiQuyetChiTietViewModel : ViewModel
    {
        private DataService _dataService;
        public HoSoDangGiaiQuyetChiTietViewModel()
        {
            _dataService = new DataService();
            ;
        }


        private List<HoSoDangGiaiQuyetChiTiet> _ketQua;
        public List<HoSoDangGiaiQuyetChiTiet> KetQua
        {
            get { return _ketQua; }
            set { SetProperty(ref _ketQua, value); }
        }


        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set { SetProperty(ref _isBusy, value); }
        }

        public override async void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            if (navigationParameter is List<HoSoChiTiet>)
            {
                int i = 1;
                var temp = (navigationParameter as List<HoSoChiTiet>).Select(item => new HoSoDangGiaiQuyetChiTiet()
                {
                    Ngayhtra = item.Ngayhtra, NgayNhan = item.NgayNhan, Sobnhan = item.Sobnhan, SoTT = i++, TenDN = item.TenDN, TenDuAn = item.TenDuAn, TrangthaiHs = item.LoaiHsDesc, Xuly = item.TTDungHanDesc,
                }).ToList();
                KetQua = temp;
            }
            else
            {


                var hoso = (HoSoDangGiaiQuyet)navigationParameter;
                IsBusy = true;
                var temp = await _dataService.ChiTietHoSo(hoso.Type, hoso.Xuly);
                foreach (var item in temp)
                {
                    item.SoTT = temp.IndexOf(item) + 1;
                }
                KetQua = temp;
                IsBusy = false;
            }

            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);

        }

        private List<TinhTrangXL> _tinhTrangXl;
        public List<TinhTrangXL> TinhTrangXl
        {
            get { return _tinhTrangXl; }
            set { SetProperty(ref _tinhTrangXl, value); }
        }
        public async Task TinhTrangXuLy(IEnumerable<object> enumerable)
        {
            dynamic t = enumerable.FirstOrDefault();
            TinhTrangXl = new List<TinhTrangXL>();
            if (t != null)
            {
                IsBusy = true;
                TinhTrangXl = await _dataService.TrangThaiHoSo(t.Sobnhan);
                await Task.Delay(1000);
                IsBusy = false;
            }


        }
    }
}
